package com.union.vehicleassistant.widget.dialog;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.hjq.base.BaseDialog;
import com.union.seekbar.IndicatorSeekBar;
import com.union.seekbar.OnSeekChangeListener;
import com.union.seekbar.SeekParams;
import com.union.vehicleassistant.R;
import com.union.vehicleassistant.aop.SingleClick;
import com.union.vehicleassistant.bean.ChannelBean;
import com.union.vehicleassistant.util.SettingUtils;
import com.union.vehicleassistant.util.UnitManager;

import java.text.MessageFormat;

/**
 * github : https://github.com/getActivity/AndroidProject
 */
public final class PreSettingDialog {

    public static final class Builder
            extends BaseDialog.Builder<Builder> {

        private final IndicatorSeekBar seekBarOne;
        private final IndicatorSeekBar seekBarTwo;
        private final TextView progressOne, progressTwo;
        @Nullable
        private OnListener mListener;

        public Builder(Context context) {
            super(context);
            setContentView(R.layout.pre_setting_dialog);
            setAnimStyle(R.style.BottomAnimStyle);

            seekBarOne = findViewById(R.id.seekbar_one);
            seekBarTwo = findViewById(R.id.seekbar_two);
            progressOne = findViewById(R.id.tv_progress_one);
            progressTwo = findViewById(R.id.tv_progress_two);

            progressOne.setText(UnitManager.getSuitPressure(125, true));
            progressTwo.setText(UnitManager.getSuitPressure(125, true));

            seekBarOne.setOnSeekChangeListener(new OnSeekChangeListener() {
                @Override
                public void onSeeking(SeekParams seekParams) {
                    String unit = SettingUtils.getInstance().getPressureUnit();
                    double progress = UnitManager.getDoublePressure(seekParams.progress);
                    progressOne.setText(MessageFormat.format("{0}/{1}({2})", progress, UnitManager.getDoublePressure(125), unit));
                }

                @Override
                public void onStartTrackingTouch(IndicatorSeekBar seekBar) {

                }

                @Override
                public void onStopTrackingTouch(IndicatorSeekBar seekBar) {

                }
            });

            seekBarTwo.setOnSeekChangeListener(new OnSeekChangeListener() {
                @Override
                public void onSeeking(SeekParams seekParams) {
                    String unit = SettingUtils.getInstance().getPressureUnit();
                    double progress = UnitManager.getDoublePressure(seekParams.progress);
                    progressTwo.setText(MessageFormat.format("{0}/{1}({2})", progress, UnitManager.getDoublePressure(125), unit));
                }

                @Override
                public void onStartTrackingTouch(IndicatorSeekBar seekBar) {

                }

                @Override
                public void onStopTrackingTouch(IndicatorSeekBar seekBar) {

                }
            });
            setOnClickListener(R.id.tv_menu_cancel, R.id.tv_menu_confirm);
        }


        public Builder setPreSettingValue(ChannelBean channel) {
            seekBarOne.setProgress(channel.getChannelOne());
            seekBarTwo.setProgress(channel.getChannelTwo());
            return this;
        }


        public Builder setListener(OnListener listener) {
            mListener = listener;
            return this;
        }

        @SingleClick
        @Override
        public void onClick(View view) {
            int viewId = view.getId();
            if (viewId == R.id.tv_menu_confirm) {
                dismiss();
                int seekBarOneProgress = seekBarOne.getProgress();
                int seekBarTwoProgress = seekBarTwo.getProgress();
                if (null != mListener) {
                    ChannelBean bean = new ChannelBean(seekBarOneProgress, seekBarTwoProgress);
                    mListener.onConfirm(getDialog(), bean);
                }

            } else if (viewId == R.id.tv_menu_cancel) {
                dismiss();
                if (mListener == null) {
                    return;
                }
                mListener.onCancel(getDialog());
            }
        }
    }

    public interface OnListener {

        /**
         * 点击确定时回调
         */
        void onConfirm(BaseDialog dialog, ChannelBean bean);

        /**
         * 点击取消时回调
         */
        default void onCancel(BaseDialog dialog) {
        }
    }
}